package com.sec.etech.bpm.cust.manager;

import cn.hutool.core.collection.CollectionUtil;
import com.sec.etech.org.service.EtechUserUtil;
import org.openbpm.base.api.exception.BusinessException;
import org.openbpm.base.core.util.StringUtil;
import org.openbpm.bpm.api.exception.BpmStatusCode;
import org.openbpm.bpm.core.dao.TaskIdentityLinkDao;
import org.openbpm.bpm.core.manager.impl.TaskIdentityLinkManagerImpl;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Set;

@Primary
@Service("etechTaskIdentityLinkManager")
public class EtechTaskIdentityLinkManager extends TaskIdentityLinkManagerImpl {

    @Resource
    TaskIdentityLinkDao taskIdentityLinkDao;

    public Boolean checkUserOperatorPermission(String userId, String instanceId, String taskId) {
        if (StringUtil.isEmpty(instanceId) && StringUtil.isEmpty(taskId))
            throw new BusinessException("检查权限必须提供流程或者任务id", BpmStatusCode.PARAM_ILLEGAL);
        // 针对流程管理员特别处理  flow_admin
        List<String> listAdmin = EtechUserUtil.getUserIdsByRoleCode("flow_admin");
        if(CollectionUtil.isNotEmpty(listAdmin)){
            for(String adminId: listAdmin){
                if(adminId.equals(userId)){
                    return true;
                }
            }
        }
        Set<String> rights = getUserRights(userId);
        return Boolean.valueOf((this.taskIdentityLinkDao.checkUserOperatorPermission(rights, taskId, instanceId) > 0));
    }
}
